<?php
namespace App\controller;
use Core\Db; 
use App\model\logModel; 
class opController
{ 
	/**
	* 请假
	*/
	public function forleave($request,$response,$args){
		$data_req = $request->getParsedBody();
		//验证数据 
		if(check(@$data_req['driver'],'required')){
			return jsonplay($response,401,'缺少参数'); 
        }
        $driver=$data_req['driver'];
		$req_id = Db::table()->get("dis_driver", "id", [
			"id" =>$driver
        ]);
        if(empty($req_id)){
            return jsonplay($response,401,'找不到信息'); 
        }
        Db::table()->update("dis_driver", [
            "status" => 4,
            "utime" => time()
        ],['id'=>$req_id]); 
        return jsonplay($response,100);   
    } 
    /**
	* 维修
	*/
	public function forfix($request,$response,$args){
		$data_req = $request->getParsedBody();
		//验证数据 
		if(check(@$data_req['code'],'required')){
			return jsonplay($response,401,'缺少参数'); 
        }
        $code=$data_req['code'];
		$req_id = Db::table()->get("dis_car", "id", [
			"code" =>$code
        ]);
        if(empty($req_id)){
            return jsonplay($response,401,'找不到信息'); 
        }
        Db::table()->update("dis_car", [
            "status" => 4,
            "utime" => time()
        ],['id'=>$req_id]); 
        return jsonplay($response,100);   
    }
    /**
	* 派车
	*/
	public function fordispatch($request,$response,$args){
        $data_req = $request->getParsedBody(); 
        $where='';
		//验证数据 
		if(is_numeric(@$data_req['people'])){ 
			$where.=" and c.maxman>=".$data_req['people'];
        }
        if(!empty(@$data_req['limcode']) || @$data_req['limcode']===0){
            $code='';
            $code_arr=explode(',',$data_req['limcode']);  
            foreach($code_arr as $v){
                if(is_numeric($v)) $code.=",".$v;
            }
            $code=trim($code,',');
			$where.=" and c.lastnum in(".$code.")";
        }
       $r = Db::table()->action(function($database) use($where) {
            $sql="select c.id as cid,c.code,d.id as did,d.name from dis_car c,dis_driver d where c.driver=d.id and c.status=1 and d.status=1 and c.finstate=1 ".$where." order by d.num asc,c.num asc,c.backtime asc,d.inyard asc,c.maxman asc,c.id asc,d.id asc limit 1 for update ";
            $r=$database->query($sql)->fetch(MYSQLI_ASSOC);
            if(empty($r)){
                $sql="select c.id as cid,c.code,d.id as did,d.name from dis_car c,dis_driver d where  c.status=1 and d.status=1 and c.finstate=1 ".$where." order by d.num asc,c.num asc,c.backtime asc,d.inyard asc,c.maxman asc,c.id asc,d.id asc limit 1 for update";
                $r=$database->query($sql)->fetch(MYSQLI_ASSOC); 
            } 
            if(empty($r)){
             return 0; 
            }
            $database->update("dis_car", [
                "status" => 2,
                "drivnow"=>$r['did'],
                "utime" => time()
            ],['id'=>$r['cid']]);
            $database->update("dis_driver", [
                "status" => 2,
                "utime" => time()
            ],['id'=>$r['did']]);
            $database->insert("dis_order", [
                "driver" => $r['did'],
                "car" => $r['cid'],
                "status" => 1,
                "ctime" => time(),
                "utime" => time()
            ]);
            return $r;
        }); 
        if(empty($r)){
            return jsonplay($response,101,'暂无合适车辆或司机');
        }
        $res_date=array('code'=>$r['code'],'driver'=>$r['name']);
        return jsonplay($response,100,'',$res_date);   
    } 
     /**
	* 出门
	*/
	public function outdoor($request,$response,$args){
		$data_req = $request->getParsedBody();
		//验证数据 
		if(check(@$data_req['code'],'required')){
			return jsonplay($response,401,'缺少参数'); 
        }
        $code=$data_req['code'];
		$res = Db::table()->get("dis_car",["status","id","drivnow"], [
			"code" =>$code
        ]);
        if($res['status']!=2){
            return jsonplay($response,501,'未知错误'); 
        }
        $res_sql=Db::table()->update("dis_car", [
            "status" => 3,
            "utime" => time()
        ],['id'=>$res['id'],'status'=>2]); 
        if($res_sql->rowCount()==0){
            return jsonplay($response,503,'未知错误'); 
        }
        Db::table()->update("dis_driver", [
            "status" => 3,
            "utime" => time()
        ],['id'=>$res['drivnow']]); 
        return jsonplay($response,100);   
    }
     /**
	* 入院
	*/
	public function indoor($request,$response,$args){
		$data_req = $request->getParsedBody();
		//验证数据 
		if(check(@$data_req['code'],'required')){
			return jsonplay($response,401,'缺少参数'); 
        }
        $code=$data_req['code'];
		$res = Db::table()->get("dis_car",["status","id","drivnow"], [
			"code" =>$code
        ]);
        if($res['status']!=3){
            return jsonplay($response,501,'未知错误'); 
        }
        $r = Db::table()->action(function($database) use($res){
            $database->update("dis_car", [
                "status" => 1,
                "utime" => time(),
                "drivnow"=>0,
                "backtime"=>time(),
                "num[+]"=>1,
            ],['id'=>$res['id']]); 
            $database->update("dis_driver", [
                "status" => 1,
                "utime" => time(),
                "num[+]"=>1,
            ],['id'=>$res['drivnow']]); 
            $database->update("dis_order", [
                "status" => 3,
                "utime" => time(),
                "otime"=>time()
            ],['driver'=>$res['drivnow'],'status'=>1]); 
        });
        return jsonplay($response,100);   
    }
 /**
	* 取消订单
	*/
	public function cancelorder($request,$response,$args){
		$data_req = $request->getParsedBody();
		//验证数据 
		if(check(@$data_req['code'],'required')){
			return jsonplay($response,401,'缺少参数'); 
        }
        $code=$data_req['code'];
		$res = Db::table()->get("dis_car",["status","id","drivnow"], [
			"code" =>$code
        ]);
        if($res['status']!=2){
            return jsonplay($response,501,'未知错误'); 
        }
        $r = Db::table()->action(function($database) use ($res){
            $database->update("dis_car", [
                "status" => 1,
                "utime" => time()
            ],['id'=>$res['id']]); 
            $database->update("dis_driver", [
                "status" => 1,
                "utime" => time()
            ],['id'=>$res['drivnow']]); 
            $database->update("dis_order", [
                "status" => 2,
                "utime" => time(),
                "otime"=>time()
            ],['driver'=>$res['drivnow'],'status'=>1]); 
        });
        return jsonplay($response,100);   
    }
     /**
	* 在途车辆
	*/
	public function driverlist($request,$response,$args){
		$data_req = $request->getParsedBody();
		 $status_arr=array(1=>'空闲', 2=>'待出车',3=>'已出车',4=>'请假');
		$res = Db::table()->select("dis_car",["status","code"], [
			"status[>]" =>1
        ]);
        foreach($res as $k=>$v){
            $res[$k]['status']=$status_arr[$v['status']];
        } 
        return jsonplay($response,100,'success',$res);   
    }
}